package com.hongning.railway.ui.main.fragments.college.exam.itemviewmodel;

import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.hongning.railway.R;
import com.hongning.railway.entity.resbean.ResAnswerCheck;
import com.hongning.railway.ui.main.fragments.college.exam.CheckAnswerFragmentViewModel;
import com.hongning.railway.util.Apputils;
import com.hongning.railway.util.SpannableStringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.goldze.mvvmhabit.base.ItemViewModel;

public class AnswerCheckItemViewModel extends ItemViewModel<CheckAnswerFragmentViewModel> {
    ObservableField<ResAnswerCheck> resAnswerCheck = new ObservableField<>();
    public ObservableField<CharSequence> name = new ObservableField<>();
    public ObservableField<CharSequence> a = new ObservableField<>();
    public ObservableField<CharSequence> b = new ObservableField<>();
    public ObservableField<CharSequence> c = new ObservableField<>();
    public ObservableField<CharSequence> d = new ObservableField<>();
    public ObservableField<CharSequence> e = new ObservableField<>();
    public ObservableField<CharSequence> f = new ObservableField<>();
    public ObservableField<String> singlechoice = new ObservableField<>("单选");
    public ObservableInt evisiable = new ObservableInt(View.GONE);
    public ObservableInt fvisiable = new ObservableInt(View.GONE);

    public AnswerCheckItemViewModel(@NonNull CheckAnswerFragmentViewModel viewModel,ResAnswerCheck data) {
        super(viewModel);
        resAnswerCheck.set(data);
        name.set(Apputils.getBlankEmpty()+data.getTopicContent());
        if (data.getFirstsd()==0){
            singlechoice.set("单选");
        }else {
            singlechoice.set("多选");
        }
        //用户选择的选项
        if (TextUtils.isEmpty(data.getSubjectOptionsE())){
            evisiable.set(View.GONE);
        }else {
            evisiable.set(View.VISIBLE);
        }
        if (TextUtils.isEmpty(data.getSubjectOptionsF())){
            fvisiable.set(View.GONE);
        }else {
            fvisiable.set(View.VISIBLE); }
        //正确为0 错误为1 就显示正确的就行了
        if (data.getResultRight()==0){
            setRightData(data);
        }else {
            setWrongData(data);
            //默认用户选择的都是错的 用正确的来覆盖错误的
            setRightData(data);
        }
    }

    private void setWrongData(ResAnswerCheck data) {
        if (!TextUtils.isEmpty(data.getStaffIdResult())&&data.getStaffIdResult().indexOf("A")!=-1){
            CharSequence optiona=SpannableStringUtils.getBuilder("")
                    .append(" ").setResourceId(R.mipmap.icon_wrong_re)
                    .append(" "+data.getSubjectOptionsA()).setForegroundColor(getColorById(R.color.red_error)).create();
            a.set(optiona);
        }else {
            a.set(" A、"+data.getSubjectOptionsA());
        }
        if (!TextUtils.isEmpty(data.getStaffIdResult())&&data.getStaffIdResult().indexOf("B")!=-1){
            CharSequence optionb=SpannableStringUtils.getBuilder("")
                    .append(" ").setResourceId(R.mipmap.icon_wrong_re)
                    .append(" "+data.getSubjectOptionsB()).setForegroundColor(getColorById(R.color.red_error)).create();
            b.set(optionb);
        }else {
            b.set(" B、"+data.getSubjectOptionsB());
        }
        if (!TextUtils.isEmpty(data.getStaffIdResult())&&data.getStaffIdResult().indexOf("C")!=-1){
            CharSequence optionc=SpannableStringUtils.getBuilder("")
                    .append(" ").setResourceId(R.mipmap.icon_wrong_re)
                    .append(" "+data.getSubjectOptionsC()).setForegroundColor(getColorById(R.color.red_error)).create();
            c.set(optionc);
        }else {
            c.set(" C、"+data.getSubjectOptionsC());
        }
        if (!TextUtils.isEmpty(data.getStaffIdResult())&&data.getStaffIdResult().indexOf("D")!=-1){
            CharSequence optiond=SpannableStringUtils.getBuilder("")
                    .append(" ").setResourceId(R.mipmap.icon_wrong_re)
                    .append(" "+data.getSubjectOptionsD()).setForegroundColor(getColorById(R.color.red_error)).create();
            d.set(optiond);
        }else {
            d.set(" D、"+data.getSubjectOptionsD());
        }
        if (!TextUtils.isEmpty(data.getStaffIdResult())&&data.getStaffIdResult().indexOf("E")!=-1){
            CharSequence optiona=SpannableStringUtils.getBuilder("")
                    .append(" ").setResourceId(R.mipmap.icon_wrong_re)
                    .append(" "+data.getSubjectOptionsE()).setForegroundColor(getColorById(R.color.red_error)).create();
            e.set(optiona);
        }else {
            e.set(" E、"+data.getSubjectOptionsA());
        }
        if (!TextUtils.isEmpty(data.getStaffIdResult())&&data.getStaffIdResult().indexOf("F")!=-1){
            CharSequence optiona=SpannableStringUtils.getBuilder("")
                    .append(" ").setResourceId(R.mipmap.icon_wrong_re)
                    .append(" "+data.getSubjectOptionsF()).setForegroundColor(getColorById(R.color.red_error)).create();
            f.set(optiona);
        }else {
            f.set(" F、"+data.getSubjectOptionsF());
        }
    }

    private void setRightData(ResAnswerCheck data){
        if (data.getResult().indexOf("A")!=-1){
            CharSequence optiona=SpannableStringUtils.getBuilder("")
                    .append(" ")
                    .setResourceId(R.mipmap.icon_right_green)
                    .append(" "+data.getSubjectOptionsA()).setForegroundColor(getColorById(R.color.green_right)).create();
            a.set(optiona);
        }else {
            if (TextUtils.isEmpty(data.getStaffIdResult())||data.getStaffIdResult().indexOf("A")==-1){
                a.set("A、"+data.getSubjectOptionsA());
            }

        }
        if (data.getResult().indexOf("B")!=-1){
            CharSequence optionb=SpannableStringUtils.getBuilder("")
                    .append(" ")
                    .setResourceId(R.mipmap.icon_right_green)
                    .append(" "+data.getSubjectOptionsB()).setForegroundColor(getColorById(R.color.green_right)).create();
            b.set(optionb);
        }else {
            if (TextUtils.isEmpty(data.getStaffIdResult())||data.getStaffIdResult().indexOf("B")==-1){
                b.set("B、"+data.getSubjectOptionsB());
            }

        }
        if (data.getResult().indexOf("C")!=-1){
            CharSequence optionc=SpannableStringUtils.getBuilder("")
                    .append(" ")
                    .setResourceId(R.mipmap.icon_right_green)
                    .append(" "+data.getSubjectOptionsC()).setForegroundColor(getColorById(R.color.green_right)).create();
            c.set(optionc);
        }else {
            if (TextUtils.isEmpty(data.getStaffIdResult())||data.getStaffIdResult().indexOf("C")==-1){
                c.set("C、"+data.getSubjectOptionsC());
            }

        }
        if (data.getResult().indexOf("D")!=-1){
            CharSequence optiond=SpannableStringUtils.getBuilder("")
                    .append(" ")
                    .setResourceId(R.mipmap.icon_right_green)
                    .append(" "+data.getSubjectOptionsD()).setForegroundColor(getColorById(R.color.green_right)).create();
            d.set(optiond);
        }else {
            if (TextUtils.isEmpty(data.getStaffIdResult())||data.getStaffIdResult().indexOf("D")==-1){
                d.set("D、"+data.getSubjectOptionsD());
            }

        }
        if (data.getResult().indexOf("E")!=-1){
            CharSequence optiona=SpannableStringUtils.getBuilder("")
                    .append(" ")
                    .setResourceId(R.mipmap.icon_right_green)
                    .append(" "+data.getSubjectOptionsE()).setForegroundColor(getColorById(R.color.green_right)).create();
            e.set(optiona);
        }else {
            if (TextUtils.isEmpty(data.getStaffIdResult())||data.getStaffIdResult().indexOf("E")==-1){
                e.set("E、"+data.getSubjectOptionsA());
            }

        }
        if (data.getResult().indexOf("F")!=-1){
            CharSequence optiona=SpannableStringUtils.getBuilder("")
                    .append(" ")
                    .setResourceId(R.mipmap.icon_right_green)
                    .append(" "+data.getSubjectOptionsF()).setForegroundColor(getColorById(R.color.green_right)).create();
            f.set(optiona);
        }else {
            if (TextUtils.isEmpty(data.getStaffIdResult())||data.getStaffIdResult().indexOf("F")==-1){
                f.set("F、"+data.getSubjectOptionsF());
            }

        }
    }


    private int getColorById(int id){
       return viewModel.getApplication().getResources().getColor(id);
    }
}
